import 'dart:typed_data';
import 'package:shen_nai/entity/data_entity.dart';

class DataUtils {
  
  //判断数据类型
  static List<DataEntity> judgeDataType(Uint8List data) {
    return formatQueryListData(cutLengthList(170, data));
  }

  //处理多条查询数据
  static List<DataEntity> formatQueryListData(List<Uint8List> data) {
    List<DataEntity> list = [];
    for (var element in data) {
      list.add(formatQueryData(element));
    }
    return list;
  }

  //处理单条查询数据
  static formatQueryData(Uint8List data) {
    //序列号ID
    String serialNumberId =
        (data[16] + (data[17] << 8) + (data[18] << 16) + (data[19] << 24))
            .toString();
    String time =
        (data[20] + (data[21] << 8) + (data[22] << 16) + (data[23] << 24))
            .toRadixString(2);
    //时间二进制补成32位
    time = "0" * (32 - time.length) + time;
    //log(time);
    //log(data.sublist(16, 24).toString());
    String year = (int.parse(
                int.parse(time.substring(0, 6), radix: 2).toRadixString(10)) +
            2021)
        .toString();
    String month = int.parse(time.substring(6, 10), radix: 2).toRadixString(10);
    String day = int.parse(time.substring(10, 15), radix: 2).toRadixString(10);
    String hour = int.parse(time.substring(15, 20), radix: 2).toRadixString(10);
    String minute =
        int.parse(time.substring(20, 26), radix: 2).toRadixString(10);
    String second =
        int.parse(time.substring(26, 32), radix: 2).toRadixString(10);
    // 质保金是否交付
    int deposit = data[24] >> 7;
    //剩余质保天数
    int residueDays = (data[24] >> 3) & 15;
    //A累计电量
    int aPower = ((data[24] & 0x07) *
            16777216 + // & 31 去除高五位 *65536*256 左移24位 24-27bit(位)
        data[25] * 65536 + // 16-23bit(位)
        data[26] * 256 + // 8-15bit(位)
        data[27]); // 0-7bit(位)
    //A累计时间
    int aTime = data[28] * 256 + data[29];

    List shisiweimoniliangData = data.sublist(30, 170);
    // 140个开关量
    List<int> switchData = List.filled(140, 0, growable: false);
    // 十四位模拟量，40个，范围0-16383
    List<int> moniliang14 = List.filled(40, 0, growable: false);
    for (int i = 0; i < 40; i++) {
      moniliang14[i] = ((shisiweimoniliangData[i * 2] & 0x3f) << 8) +
          shisiweimoniliangData[i * 2 + 1];
      switchData[i * 2] = shisiweimoniliangData[i * 2] >> 7;
      switchData[i * 2 + 1] = (shisiweimoniliangData[i * 2] >> 6) & 1;
    }
    // 七位模拟量，60个，范围0-127
    List<int> moniliang7 = List.filled(60, 0, growable: false);
    for (int i = 0; i < 30; i++) {
      moniliang7[i * 2] = ((shisiweimoniliangData[i * 2 + 80] & 0x3f) << 1) +
          ((shisiweimoniliangData[i * 2 + 81] & 0x80) >> 7);
      moniliang7[i * 2 + 1] = shisiweimoniliangData[i * 2 + 81] & 0x7f;
      switchData[i * 2 + 80] = shisiweimoniliangData[i * 2 + 80] >> 7;
      switchData[i * 2 + 81] = (shisiweimoniliangData[i * 2 + 80] >> 6) & 1;
    }
    // B累计电量
    int bPower = moniliang14[38] * 10000 + moniliang14[39];
    // B累计时间
    int bTime = moniliang14[32] + ((moniliang14[37] & 0x3000) << 2);

    //log('$year-$month-$day $hour-$minute-$second     消息ID：' + serialNumberId);

    DataEntity dataEntity = DataEntity(
        serialNumberId,
        aPower.toString(),
        aTime.toString(),
        bPower.toString(),
        bTime.toString(),
        moniliang14,
        moniliang7,
        switchData,
        dateTime: DateTime(int.parse(year), int.parse(month), int.parse(day),
            int.parse(hour), int.parse(minute), int.parse(second)),
        deposit: deposit,
        residueDays: residueDays);
    return dataEntity;
  }

  //切割固定长度的list
  static cutLengthList(int len, Uint8List list) {
    var length = list.length; //列表数组数据总条数
    List<Uint8List> result = []; //结果集合
    int index = 1;
    //循环 构造固定长度列表数组
    while (true) {
      if (index * len < length) {
        Uint8List temp =
            Uint8List.fromList(list.skip((index - 1) * len).take(len).toList());
        result.add(temp);
        index++;
        continue;
      } else {
        break;
      }
      // Uint8List temp =
      //     Uint8List.fromList(list.skip((index - 1) * len).toList());
      // result.add(temp);
    }
    return result;
  }
}
